package com.lizhi.typehandler.function;

import com.alibaba.fastjson.JSON;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ListGetNullableResult {
    public static <T> List<T> listGetNullAbleResult(ResultSet rs, String columnName, Class<T> tClass) throws SQLException {
        String json = rs.getString(columnName);
        if (json == null) {
            return new ArrayList<>();
        }
        return JSON.parseArray(json, tClass);
    }

    public static <T> List<T> listGetNullAbleResult(ResultSet rs, int columnIndex, Class<T> tClass) throws SQLException {
        String json = rs.getString(columnIndex);
        if (json == null) {
            return new ArrayList<>();
        }
        return JSON.parseArray(json, tClass);
    }

    public static <T> List<T> listGetNullAbleResult(CallableStatement rs, int columnIndex, Class<T> tClass) throws SQLException {
        String json = rs.getNString(columnIndex);
        if (json == null) {
            return new ArrayList<>();
        }
        return JSON.parseArray(json, tClass);
    }
}
